package com.pri.chat.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.luck.picture.lib.entity.LocalMedia;
import com.netease.nim.uikit.common.util.string.StringUtil;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.DTBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.activity.MyVedioActivity1;
import com.pri.chat.activity.VedioMsgActivity;
import com.pri.chat.adapter.BangDanVedioAdapter;
import com.pri.chat.adapter.VideoListPAdapter;
import com.pri.chat.event.EventTag;
import com.pri.chat.event.MessageEvent;
import com.pri.chat.model.DTModel;
import com.pri.chat.utils.Des3Util;
import com.pri.chat.utils.StaggeredDividerItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.fragment.FragmentLazy;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/7
 */
public class BangDanVideoFragment extends FragmentLazy implements  BaseQuickAdapter.OnItemClickListener{
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.none)
    ImageView none;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    private int type; // 0实时榜；1--昨日榜单
    private String mOtherId ;

    public static BangDanVideoFragment getInstance(int type) {
        BangDanVideoFragment fragment = new BangDanVideoFragment();
        fragment.type = type;
        return fragment;
    }

    Unbinder unbinder;

    @Override
    protected View initViews(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = layoutInflater.inflate(R.layout.fragment_dt_layout, viewGroup, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        initRec();
        initSrl();
        return view;
    }

    public void deleteVedio(DTBean dt){
        mList.remove(dt);
        mAdapter.notifyDataSetChanged();
    }

    public void updateVedio(DTBean dt){
        for(int i=0;i<mList.size();i++){
            if(dt.getId().equals(mList.get(i).getId())){
                mList.set(i,dt);
                mAdapter.notifyDataSetChanged();
                break;
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(MessageEvent messageEvent) {
        if(messageEvent.getTag().equals(EventTag.DT)){
            refresh();
        }

    }

    public void refresh() {
        isShowDialog = false;
        page = 1;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        initData();
    }

    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
        srlAll.setOnLoadmoreListener(refresh_layout -> {
            refresh_layout.finishLoadmore(1000);
            initData();
        });
    }
    private boolean isShowDialog = true;
    private int page = 1;
    private List<DTBean> mList;
    private BangDanVedioAdapter mAdapter;
    private void initRec(){
        mList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager =new LinearLayoutManager(mContext);
        srlAll.setEnableLoadmore(false);
        srlAll.setEnableRefresh(false);
        recMain.setLayoutManager(linearLayoutManager);
        mAdapter = new BangDanVedioAdapter(R.layout.item_bang_dan, mList,type);
        recMain.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        SubscriberOnNextListener<BaseBean<List<DTBean>>> onNextListener = baseBean -> {
            if(baseBean.getData().isEmpty()&&page==1){
                none.setVisibility(View.VISIBLE);
                mAdapter.notifyDataSetChanged();
                RxToast.normal(getString(R.string.no_data));
            }else {
                none.setVisibility(View.INVISIBLE);
                mList.addAll(baseBean.getData());
                for(int i=0;i<mList.size();i++){
                    List<String> list =new ArrayList<>();
                    List<LocalMedia> mediaList =new ArrayList<>();
                    if(!StringUtil.isEmpty(mList.get(i).getPicList())){
                        String imgs[] = mList.get(i).getPicList().split(",");
                        for(int j=0;j<imgs.length;j++){
                            LocalMedia localMedia=new LocalMedia();
                            list.add(imgs[j]);
                            localMedia.setPath(imgs[j]);
                            mediaList.add(localMedia);
                        }
                    }
                    mList.get(i).setImgs(list);
                    mList.get(i).setMediaList(mediaList);
                }
                mAdapter.notifyDataSetChanged();
            }
        };
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",SharedHelper.readId(mContext));
        params.put("type",type);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(params));
        HttpMethods.getInstance().listDynamicPaiHangBang(new ProgressSubscriber<>(onNextListener, mContext, isShowDialog), requestBody);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        unbinder.unbind();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Bundle bundle=new Bundle();
        bundle.putString("id",mList.get(position).getId());
        RxActivityTool.skipActivity(mContext, VedioMsgActivity.class,bundle);
    }
}
